﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic;
using System.Web;
using System.Web.Mvc;
using SDT.Model;
using SDT.Lib.Paging;
using SDT.Site.ViewModels;
using SDT.Site.Properties;
using SDT.Lib.Common;
namespace SDT.Site.Controllers
{
    public class EventController : Controller
    {
        SDTDataContext eDataContext = new SDTDataContext();
        //
        // GET: /Event/Details/5

        public ActionResult Details(int id, int? page)
        {
            var pageNumber = Convert.ToInt32(UserState.getUserStateFromRequest("event.PageNumber", "page", 1));
            var pageIndex = pageNumber - 1;
            var pageSize= Utils.ParseInt(Settings.Default.PageSize);
            EventDetailModels e = new EventDetailModels();
            e.Event = eDataContext.Events.Where(c=>c.ID == id).SingleOrDefault();
            e.Event.nArticle += 1;
            eDataContext.SubmitChanges();
            e.Articles = (from a in e.Event.ArticleEventMaps orderby a.ArticleID descending select a.Article).ToPagedList(pageIndex, pageSize);
            e.ImageUrl = Settings.Default.SiteRootUrl + Settings.Default.ArtileImagePreviewUrlPrefix;
            e.ImagePath = Settings.Default.SiteRootPath + Settings.Default.ArticleImageRootPath;
            return View(e);
        }
        [ChildActionOnly]
        public ActionResult Gets(int number, string viewName, string title)
        {
            var ev = new EventListModels();
            ev.Events = eDataContext.Events
                          .Where(e => e.PubYN == true)
                          .OrderBy(string.Format("{0} {1}", "ID", "desc")).Take(number).ToList();
            ev.ImagePath = Settings.Default.SiteRootPath + Settings.Default.EventImageRootPath;
            ev.ImageUrl = Settings.Default.SiteRootUrl + Settings.Default.EventRootUrl;
            ViewData["Title"] = title;
            return PartialView(viewName,ev);
        }
        [ChildActionOnly]
        public ActionResult Lasted(int number, int catid)
        {
            var ev = new EventListModels();
            var listArr = catid > 0 ? ArticleManager.GetChild(catid) : new List<int>();
            ev.Events = eDataContext.Events
                          .Where(e => e.PubYN == true)
                          .Where(e => catid <= 0 ? true : e.EventArticleCategoryMaps.Where(m => listArr.Contains(m.CategoryID)).ToList().Count > 0)
                          .OrderBy(string.Format("{0} {1}", "ID", "desc")).Take(number).ToList();
            ev.ImagePath = Settings.Default.SiteRootPath + Settings.Default.EventImageRootPath;
            ev.ImageUrl = Settings.Default.SiteRootUrl + Settings.Default.EventRootUrl;
            return View(ev);
        }
        [ChildActionOnly]
        public ActionResult LastedSmall(int number, int catid)
        {
            var ev = new EventListModels();
            var listArr = catid > 0 ? ArticleManager.GetChild(catid) : new List<int>();
            ev.Events = eDataContext.Events
                          .Where(e => e.PubYN == true)
                          .Where(e => catid <= 0 ? true : e.EventArticleCategoryMaps.Where(m => listArr.Contains(m.CategoryID)).ToList().Count > 0)
                          .OrderBy(string.Format("{0} {1}", "ID", "desc")).Take(number).ToList();
            ev.ImagePath = Settings.Default.SiteRootPath + Settings.Default.EventImageRootPath;
            ev.ImageUrl = Settings.Default.SiteRootUrl + Settings.Default.EventRootUrl;
            return View(ev);
        }
    }
}
 